﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication18
{
    class Program
    {
        static void Main(string[] args)
        {
            Client _client = new Client(1000, "Sentjurv", "Sergey", new DateTime(1991, 04, 23), "KB1417047", "Mogilev");
            foreach (var i in _client.Accounts)
            {
                Console.WriteLine("счет номер {0}, оформленный на {1}, с капиталом {2} и ссылкой на банк {3}", i.Number, i.Owner.Info.LastName, i.Money, i.Owner.BankInfo);
            }

            Bank bank = new Bank();
            Client newClient = bank.AddClient(_client);
            Account newAccount1 = bank.AddAccount(newClient, 1500);
            Account newAccount2 = bank.AddAccount(newClient, 1300);

          //bank.AddAccount(new Client(1000, "Sentjurv", "Sergey", new DateTime(1991, 04, 23), "KB1417047", "Mogilev"), 1000);

            foreach (var i in newClient.Accounts)
            {
                Console.WriteLine("счет номер {0}, оформленный на {1}, с капиталом {2} и ссылкой на банк {3}", i.Number, i.Owner.Info.LastName, i.Money, i.Owner.BankInfo);
            }

            Console.WriteLine();
            Console.WriteLine("на первом счету {0}, на втором {1}", newAccount1.Money, newAccount2.Money);
            bank.TrasferMoney(200, newAccount1, newAccount2);
            Console.WriteLine("на первом счету {0}, на втором {1}", newAccount1.Money, newAccount2.Money);

            Console.ReadLine();
        }
    }

    public class Bank
    {
        private List<Account> _accounts = new List<Account>();
        public List<Account> Accounts
        {
            get
            {
                return _accounts;
            }
        }

        private List<Client> _clients = new List<Client>();
        public List<Client> Clients
        {
            get
            {
                return _clients;
            }
        }

        public Client AddClient(Client _client)
        {
            Client newClient = _client;
            newClient.BankInfo = this;
            _clients.Add(newClient);

            return newClient;
        }

        public Account AddAccount(Client _client, Double _money)
        {
            if (_client.BankInfo == null) throw new Exception("невозможно создать счет на незарегистрированного в банке клиента");

            Account newAccount = _client.AddAccount(_money);
            _accounts.Add(newAccount);

            return newAccount;
        }

        public void AddMoney(Double _money, Account _account)
        {
            _account.Money += _money;
        }

        public void RemoveMoney(Double _money, Account _account)
        {
            _account.Money -= _money;
        }

        public void TrasferMoney(Double _money, Account _fromAccoun, Account _toAccount)
        {
            _fromAccoun.Money = _fromAccoun.Money - _money;
            _toAccount.Money = _toAccount.Money + _money;
        }
    }

    public class Account
    {
        public Account(Double _money, DateTime _startDate)
        {
            Number = _number++;
            Money = _money;
            StartDate = _startDate;
        }

        //private Client _client;
        private static Int32 _number = 1000;

        public Int32 Number { get; set; }
        public Double Money { get; set; }
        public DateTime StartDate { get; set; }
        public Client Owner { get; set; }
    }

    public class Client
    {
        //внечение изменений
        private Bank _tempBank = null;

        public Account AddAccount(Double _money)
        {
            Account _account = new Account(_money, DateTime.Now);

            //внесение изменений
            if (_tempBank != BankInfo)
            {
                _tempBank = BankInfo;

                _accounts.Clear();
                _accounts.Add(_account);
                _account.Owner = this;

                return _account;
            }

            else
            {
                _tempBank = BankInfo;

                _accounts.Add(_account);
                _account.Owner = this;

                return _account;
            }


          //  _accounts.Add(_account);
          //  _account.Owner = this;
  
           // return _account;
        }

        public Client(Double _money, String _lastName, String _firstName, DateTime _birthday, String _pasport, String _adress)
        {
            Info = new ClientInfo(_lastName, _firstName, _birthday, _pasport, _adress);

            Account _account = new Account(_money, DateTime.Now);
            _accounts.Add(_account);
            _account.Owner = this;
        }

        public ClientInfo Info { get; set; }
        public Bank BankInfo { get; set; }

        private List<Account> _accounts = new List<Account>();
        public List<Account> Accounts
        {
            get
            {
                return _accounts;
            }
        }
    }

    public class ClientInfo
    {
        public ClientInfo(String _lastName, String _firstName, DateTime _birthday, String _pasport, String _adress)
        {
            LastName = _lastName;
            FirstName = _firstName;
            Birthday = _birthday;
            Pasport = _pasport;
            Adress = _adress;
        }

        public String LastName { get; set; }
        public String FirstName { get; set; }
        public DateTime Birthday { get; set; }
        public String Pasport { get; set; }
        public String Adress { get; set; }
    }
}
